package com.magicode.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.EnterpriseFestivalRela;
import com.magicode.cms.entiy.base.Festival;
import com.magicode.cms.entiy.base.NewsFestivalRela;
import com.magicode.cms.entiy.base.NewsPersonRela;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.portal.service.ISecFestivalService;

@Service("secFestivalService")
@Transactional
public class SecFestivalServiceImpl implements ISecFestivalService {

	@Override
	public Festival get(String id) {
		String hql = " from "+Festival.class.getName()+" f "
				+ " left join fetch f.mainPic "			//主图片
				+ " left join fetch f.festivalDic "		//节展名称
				+ " left join fetch f.year "			//年度
				+ " left join fetch f.area "			//地区
//				+ " left join fetch f.organizer "		//主办机构
				+ " left join fetch f.subject "		    //主题
				+ " left outer join fetch f.relaPics "
				+ " where f.id=? ";
		Festival festival = null;
		try {
			festival = (Festival) this.commonDaoService.getByHQL(hql, new String[]{id});
			//协办机构
			List<Enterprise> entList = new ArrayList();
			entList = this.coorganizerList(id);
			festival.setCoorganizerList(entList);
			//主办机构
			List<EnterpriseFestivalRela> entfestList = new ArrayList();
			entfestList = this.orgRelaList(id);
			festival.setOrgRelaList(entfestList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return festival;
	}

	/**
	 * 根据id获取当前影视节展的协办机构
	 * @param id
	 * @return
	 */
	public List<Enterprise> coorganizerList(String id){
		List<Enterprise> entList = new ArrayList();
		String hql = " select e from "+Enterprise.class.getName()+" e ,  "+Festival.class.getName()+" f "
				+" where e in elements(f.coorganizerList) and f.id = :id order by e.id desc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			entList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return entList;
	}
	
	/**
	 * 根据id获取当前影视节展的主办机构
	 * @param id
	 * @return
	 */
	public List<EnterpriseFestivalRela> orgRelaList(String id){
		List<EnterpriseFestivalRela> entList = new ArrayList();
		String sql = " select efr from "+EnterpriseFestivalRela.class.getName()+" efr left outer join fetch efr.ent e left join efr.festival f "
				+ " where f.id = :id order by efr.orderCode asc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try{
			entList = this.commonDaoService.queryList(sql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return entList;
	}
	
	@Override
	public List<Article> getArtList(String id) {
		List<Article> artList = new ArrayList();
		String hql = " select n from "+NewsFestivalRela.class.getName()+" nf left join nf.festival f , "+Article.class.getName()+" n " 
				+ " where f.id = :id and nf.news = n and n.status = 2 and n.valid = 1 order by n.subcribeTime desc , nf.orderCode asc  ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			artList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return artList;
	}
	
	@Override
	public List<Festival> getFestList(Festival festival) {
		List<Festival> festList = new ArrayList();
		String id = festival.getFestivalDic().getId();
		String hql = " select f from "+Festival.class.getName()+" f left join f.festivalDic d left join f.year y " 
				+ " where f.festivalDic.id = :id order by y.title desc , y.orderCode asc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			festList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return festList;
	}
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;

}
